package com.xx.service.impl;

import com.xx.dao.*;
import com.xx.domain.Role;
import com.xx.domain.User;
import com.xx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @Author : 徐闯 ssm_admin
 * @Date : 2021-05-17 11:05:35
 * @Description : 用户操作
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private FindUserMapper findUserMapper;
    @Autowired
    private DeleteUserMapper deleteUserMapper;
    @Autowired
    private UpdateUserMapper updateUserMapper;
    @Autowired
    private InsertUserMapper insertUserMapper;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private FindRoleMapper findRoleMapper;
    @Autowired
    private InsertRoleMapper insertRoleMapper;
    @Override
    @Transactional
    public int lockUser(User user) {
        return updateUserMapper.updateStatus(user);
    }

    @Override
    @Transactional
    public int deleteUser(int uId) {
        int delUser = deleteUserMapper.deleteOne(uId);
        int delUserRole = deleteUserMapper.deleteOneUserRole(uId);
        return delUser+delUserRole;
    }

    @Override
    @Transactional
    public int deleteAll(int[] uId) {
        return deleteUserMapper.deleteAll(uId)+deleteUserMapper.deleteAllUserRole(uId);
    }

    @Override
    @Transactional
    public int insertUser(User user) {
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        return insertUserMapper.insertUser(user);
    }

    @Override
    public User userDetails(int uId) {
        return findUserMapper.findUserAndRole(uId);
    }

    @Override
    public List<Role> showROle(int uId) {
        return findRoleMapper.findList(uId);
    }

    @Override
    public int insertUserRole(HashMap map) {
        System.out.println(map.get("uId"));
        System.out.println(map.get("rList"));
        return insertRoleMapper.insertUserRole(map);
    }
}
